package edu.kaist.cs442.server.locationalpush.push.c2dm;

import edu.kaist.cs442.server.locationalpush.common.mobile.MobileDAO;
import edu.kaist.cs442.server.locationalpush.common.mobile.MobileVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import sun.rmi.runtime.Log;

import java.util.List;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: duyleekun
 * Date: 2/29/12
 * Time: 10:48 PM
 * To change this template use File | Settings | File Templates.
 */

@Configurable
public class PushSVCImpl implements PushSVC {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired(required = true)
	private MobileDAO mobileDAO;

	public PushSVCImpl() {
	}


	@Override
    public int sendTo(MessageVO message, String uuid) {
        return (GoogleHelper.sendMessage(mobileDAO.getRegistrationId(uuid), message));
    }

	@Override
	public int sendNearby(MessageVO message, double latitude, double longitude, double diameter) {

		log.info(String.format("Broadcast to %f %f %f", latitude, longitude, diameter));
		List<MobileVO> nearbyPhones = mobileDAO.getNearby(latitude, longitude, diameter);
		int count = 0;
		for (MobileVO mobile : nearbyPhones)
		{
			log.info(mobile);
			if (GoogleHelper.sendMessage(mobile.getRegistrationId(),message) != -1)
				count++;
		}
		log.info(count);
		return count;
	}
}
